#include <reg52.h>
sbit Buzz1 = P1 ^ 5;
sbit LED = P2 ^ 1;
bit enable = 0, pause = 0;
int ZKB = 0, a = 0, b = 1, TEMP = 0;
unsigned int T0RH = 0xff, T0RL = 0xff, beat = 0, time = 0, beattime = 0, soundtime = 0;
unsigned int code tone[] = {
    1046, // mid  1  (0)
    1174, // mid  2  (1)
    1318, // mid  3  (2)
    1396, // mid  4  (3)
    1568, // mid  5  (4)
    1760, // mid  6  (5)
    1976, // mid  7  (6)
    2094, // high 1 (7)
    2350, // high 2 (8)
    2638, // high 3 (9)
    2794, // high 4 (10)
    3136, // high 5 (11)
    3520, // high 6 (12)
    3952, // high 7 (13)
    524,  // low  1  (14)
    588,  // low  2  (15)
    660,  // low  3  (16)
    698,  // low  4  (17)
    784,  // low  5  (18)
    880,  // low  6  (19)
    988}; // low 7  (20)
unsigned char code music[] = {
    4, 9, 4, 4, 8, 9, 4, 4, 4, 7, 8,
    4, 5, 7, 5, 8, 4, 5, 7, 5, 7,
    7, 7, 8, 7, 8, 9, 11, 9, 11, 9, 7, 9, 8,
    9, 8, 8, 9, 8, 9, 9, 5, 7, 9, 8, 8, 9, 5, 4, 5, 7,
    2,
    2, 4,
    0, 0, 0, 2, 4, 2, 4, 4, 4, 4, 5, 2, 1, 0, 3, 4, 2,
    1, 1, 1, 2, 3, 3, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 2, 4, 2, 3, 4, 5, 5, 5, 5, 5, 7, 2, 4,
    3, 3, 3, 3, 5, 2, 2, 2, 2, 4, 1, 1, 1, 2, 4, 20, 0,
    0, 0, 0, 0, 2, 4, 2, 3, 4, 5, 5, 5, 5, 5, 3, 5, 4,
    3, 3, 3, 5, 2, 2, 2, 2, 4, 1, 1, 1, 2, 1, 0, 1, 0,
    0, 0, 0, 2, 4, 4, 2, 4, 0, 5, 5, 5, 5, 5, 7, 5, 4,
    3, 3, 3, 5, 2, 2, 2, 2, 2, 4, 1, 1, 1, 2, 4, 20, 0,
    0, 0, 0, 0, 0, 2, 4, 4, 2, 4, 0, 5, 5, 5, 5, 5, 7, 5, 4,
    3, 3, 3, 5, 2, 2, 2, 2, 2, 4, 1, 1, 1, 2, 4, 20, 0,
    0, 0, 0, 2, 4, 5, 5, 3, 5, 4, 3, 3, 3, 5, 2, 4,
    1, 2, 4, 6, 0, 0, 0, 0, 0, 2, 4, 4, 2, 4, 0,
    5, 5, 5, 5, 5, 7, 5, 4, 3, 3, 3, 5, 2, 2, 2, 2, 2, 4,
    1, 1, 1, 2, 4, 6, 0, 0, 0, 0, 2, 4, 4,
    5, 5, 3, 5, 4, 3, 3, 3, 5, 2, 4,
    1, 2, 4, 20, 0,
    0, 0, 0, 2, 4, 5, 5, 3, 5, 4, 3, 3, 3, 5, 2, 4,
    1, 2, 4, 6, 0, 0, 0, 0, 0, 2, 4, 4, 2, 4, 0,
    5, 5, 5, 5, 5, 7, 5, 4, 3, 3, 3, 5, 2, 2, 2, 2, 2, 4,
    1, 1, 1, 2, 4, 6, 0, 0, 0, 0, 2, 4, 4,
    5, 5, 3, 5, 4, 3, 3, 3, 5, 2, 4,
    1, 2, 4, 20, 0,
    0, 0, 0, 0, 2, 4,
    5, 5, 5, 7, 5, 4, 3, 3, 3, 3, 3, 1, 3, 2, 2, 2, 2, 2, 4, 2,
    1, 2, 4, 20, 0,
    3, 3, 3, 5, 2, 4, 1, 0, 0
    //----end
};
unsigned char code rhythm[] = {
    4, 6, 2, 4, 2, 2, 4, 2, 2, 2, 8,
    2, 2, 2, 2, 8, 2, 2, 2, 2, 16,
    4, 4, 4, 4, 1, 15, 2, 2, 2, 2, 4, 4, 16,
    2, 2, 2, 2, 2, 2, 4, 5, 11, 2, 2, 2, 2, 4, 2, 2, 16,
    16,
    1, 1,
    2, 2, 2, 2, 2, 2, 4, 1, 1, 1, 1, 1, 1, 2, 1, 1, 6,
    3, 1, 3, 1, 3, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 8,
    2, 1, 1, 2, 2, 2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 1
{

    TH0 = T0RH;
    TL0 = T0RL;

    pause = 1;
    if (enable)
    {
        Buzz1 = ~Buzz1;
    }
    else
    {
        Buzz1 = 1;
    }
}
void _LED() interrupt 3
{
    TH1 = 0xD8;
    TL1 = 0xF0;
    if (a > 200)
    {
        a = 0;
    }
    if (a >= ZKB)
    {
        LED = 1;
    }
    else
    {
        LED = 0;
    }
    a++;
}
void main()
{
    unsigned int i;
    EA = 1;
    TMOD = 0X11;
    IT0 = 0;
    ET0 = 1;
    TR0 = 1;
    // TL1 = 0xB0; // 设置定时初值
    // TH1 = 0x3C; // 设置定时初值
    // TF1 = 0;    // 清除TF1标志
    // ET1 = 1;
    // IT1 = 0;
    // TR1 = 1; // 定时器1开始计时
    LED = 0;

    while (1)
    {
        for ( i = 0; i < 2048; i++)
            ;
        for (beat = 0; beat < sizeof(music);)
        {
            while (pause == 0)
                ;
            pause = 0;
            if (time == 0)
            {
                T0RH = 65536 - (1000000) / (tone[music[beat]]) >> 8;
                T0RL = 65536 - (1000000) / (tone[music[beat]]);
                beattime = (rhythm[beat] * tone[music[beat]]) / 8;
                soundtime = beattime * 0.75;
                enable = 1;
                time++;
            }
            else
            {
                if (time >= beattime)
                {
                    time = 0;
                    beat++;
                }
                else
                {
                    time++;
                    if (time == soundtime)
                        enable = 0;
                }
            }
        }
        for ( i = 0; i < 32768; i++)
            ;
        // if (b)
        // {
        //     TEMP++;
        //     if(TEMP)

        // }
        // else
        // {
        //     ZKB--;
        // }
        // if (a <= 0)
        // {
        //     b = 1;
        // }
        // else if (a >= 100000)
        // {
        //     b = 0;
        // }
    }
}
